package com.mes.basicdata.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.bstek.dorado.data.entity.EntityUtils;
import com.mes.basicdata.dao.iface.ICalendarDataDao;
import com.mes.basicdata.domain.CalendarData;
import com.mes.common.dao.BasicDao;

@Repository
public class CalendarDataDao extends BasicDao<CalendarData> implements ICalendarDataDao {

	/**
	 * @author lcs
	 * @see the method use for ScheduleCalendarManager
	 * @param calendarUid
	 * @return
	 * @throws Exception
	 */
	public List<CalendarData> getWeekDayCalendarData(String calendarUid) throws Exception {

		List<CalendarData> calendarList = new ArrayList<CalendarData>();

		Session session = this.getSession();
		/*
		 * from com.mes.basicdata.persistence.domain.CalendarData where
		 * calendarUid=:calendarUid and dayOrException>0 and dayOrException<8
		 * order by dayOrException
		 */
		Criteria crit = session.createCriteria(CalendarData.class);
		crit.add(Restrictions.eq("calendarUid", calendarUid));
		crit.add(Restrictions.gt("dayOrException", 0l));
		crit.add(Restrictions.lt("dayOrException", 8l));
		crit.addOrder(Order.asc("dayOrException"));
		calendarList = crit.list();
		if (calendarList == null)
			return null;
		return calendarList;
	}

	/**
	 * @author lcs
	 * @see the method use for ScheduleCalendarManager
	 * @param calendarUid
	 * @param fromDate
	 * @param toDate
	 * @return
	 * @throws Exception
	 */
	public List<CalendarData> getExceptionCalendarData(String calendarUid, Date fromDate, Date toDate)
			throws Exception {

		if (fromDate == null || toDate == null)
			throw new Exception("寮�濮嬫椂闂村拰缁撴潫鏃堕棿閮戒笉鑳戒负绌猴紒");
		List<CalendarData> calendarDataList = new ArrayList<>();
		try {
			Session session = this.getSession();
			/*
			 * from com.mes.basicdata.persistence.domain.CalendarData where
			 * calendarUid=:calendarUid and dayOrException=0 and
			 * not(effective=0) and (fromDate between :fromDate and :toDate or
			 * toDate between :fromDate and :toDate or fromDate <=:fromDate and
			 * toDate>=:toDate) order by fromDate
			 */
			Criteria crit = session.createCriteria(CalendarData.class);
			crit.add(Restrictions.eq("calendarUid", calendarUid));
			crit.add(Restrictions.eq("dayOrException", 0l));
			crit.add(Restrictions.ne("effective", false));
			crit.add(Restrictions.or(Restrictions.between("fromDate", fromDate, toDate), Restrictions.or(
					Restrictions.between("toDate", fromDate, toDate),
					Restrictions.and(Restrictions.le("fromDate", fromDate), Restrictions.ge("toDate", toDate)))));

			calendarDataList = crit.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (calendarDataList == null)
			return null;
		return calendarDataList;
	}
	
	@Override
	public void saveCalendarData(Set<CalendarData> calendarDatas) throws Exception {
		for (CalendarData c : calendarDatas) {
			switch (EntityUtils.getState(c)) {
			case MODIFIED:
				this.update(c);
				break;
			case DELETED:
				this.delete(c);
				break;
			case NEW:
				this.add(c);
				break;
			default:
				break;
			}
		}
		
	}
}
